<template>
	<view>
		<view class="body">
			<view class="kongs"></view>
			<scroll-view scroll-y="true" style="width: 100%;height:100vh;" @scrolltolower="scrollLower">
				<view class="knobble dis dis6">
					<block v-for="(item,indexs) in goodList" :key="indexs">
						<view class="items dis dis5" @click="goProduct(item.id)">
							<view class="itmeImg">
								<image :src="item.pic" mode="aspectFit"></image>
							</view>
							<view class="itemText">{{item.title}}</view>
							<view class="bootm dis">
								<block v-for="(i,index1) in item.gongxiao" :key="index1">
									<view class="bootmItem contenr" v-if="index1<=1">{{i}}</view>
								</block>
							</view>
						</view>
					</block>
				</view>
			</scroll-view>
		</view>

		<view class="yangKong"></view>
		<view v-if="!isLogin_s && showIs">
			<log @fnnc="func"></log>
		</view>
		<tarBar :indexTo="2"></tarBar>
	</view>
</template>

<script>
	let app = getApp().globalData;
	import tarBar from '@/components/tarbar.vue';
	import ctiy from '@/common/js/city.js';
	import log from '@/components/login.vue';
	import indexFx from '@/common/js/indexFx.js';
	import tools from '@/common/js/tools.js';
	export default {
		components: {
			'tarBar': tarBar,
			log
		},
		data() {
			return {
				list: ctiy.list,
				listIndex: 0,
				weiDuIndex: 0,
				ty: '新品',
				weiDu: [],
				freight: [], //全部商品Id
				page: 1, //当前页
				pagesize: 10, //页码
				step: 0, //下一次要从哪取
				is: true, //是否可以下拉
				goodList: [], //商品数据
				lastidData: 0, //最后一条记录id
				weiduid: 0, //维度id
				isLogin_s: true, //用户是否登录
				showIs: false, //是否显示登录窗口
				strDate: '',
				isdetail :false	,		//是否是详情页
			};
		},
		onShow() {
			this.strDate = new Date().getTime();
			if(this.isdetail){
				console.log('ssss')
				this.isdetail = false;
				return
			}
			console.log('cccc')
			if (!app.user.sex || !uni.getStorageSync('userInfo')) {
				console.log(app)
				this.isLogin_s = false;
			} else {
				this.isLogin_s = true;
			}
			
			this.getInitialData()
			this.is=true;
			this.page = 1; //当前页
			this.pagesize = 10; //页码
			this.step = 0;
			this.goodList = [];
		},
		onHide() {
			let endDate = new Date().getTime();
			let seconds = Math.round((endDate - this.strDate) / 1000);
			indexFx.basePoint(2, seconds)
			// console.log(`时间差${seconds}秒`);
		},
		onLoad() {
			 

		},

		methods: {
			// 选择新品推荐标签
			setlistIndex(index, ty) {
				if (!this.isLogin_s) {
					this.showIs = true;
					return
				}
				this.listIndex = index;
				this.weiDuIndex = 0;
				this.ty = ty;
				this.page = 1;
				this.pagesize = 10,
				this.step = 0;
				this.goodList = [];
				this.lastidData = 0;
				if (ty == '新品') {
					this.getAll(this.page, this.pagesize, this.step)
				} else {
					this.getAroduct(this.ty, 0, 0);
				}
				console.log(ty)
			},
			// 选择维度标签
			selWdIndex(index, id) {
				if (!this.isLogin_s) {
					this.showIs = true;
					return
				}
				this.weiDuIndex = index;
				this.weiduid = id;
				this.lastidData = 0;
				this.goodList = [];
				this.getAroduct(this.ty, this.lastidData, id);
			},
			// 获取初始数据
			getInitialData() {
				let that = this;
				this.$http._post('examination/getconsumerrndlist.do').then(res => {
					console.log(res)
					if (res.data.issuccess == 1) {
						let {
							data
						} = res.data;
						that.freight = data;
						that.getAll(that.page, that.pagesize, that.step);
					}

				})
			},
			getAll(page, pagesize, step) {
				let stepZize = page * pagesize;
				let freight = this.freight;
				let len = freight.length;
				let list = [];
				for (let i = step; i < stepZize; i++) {
					list.push(freight[i])
				}
				this.step = stepZize;
				if (list.length <= 0) {
					console.log('长度wei空')
					this.$http.isMssage('暂无更多记录')
					this.is = false;
					return
				}
				let leng = list.length;
				let str = '';
				for (let i = 0; i < leng; i++) {
					str = str + `${list[i]},`
				}
				str = (str.substring(str.length - 1) == ',') ? str.substring(0, str.length - 1) : str;
				// console.log('str',str)
				this.getGoods(str);

			},
			getGoods(idlist) {
				let that = this;
				let goodList = this.goodList;
				this.$http._post('examination/getconsumerlistbyidlist.do', {
					idlist
				}).then(res => {
					if (res.data.issuccess == 1) {
						let {
							data
						} = res.data;
						let len = data.length;
						if (len == 0) {
							that.is = false
						}
						for (let i = 0; i < len; i++) {
							data[i].gongxiao = data[i].gongxiao.split('\n')
						}
						if (goodList.length <= 0) {
							that.goodList = data;
						} else {
							goodList.push.apply(goodList, data);
							that.goodList = goodList;
						}

					}
					console.log(res)
				})
			},
			// 获取除新品外的商品数据
			getAroduct(type, lastid, weiduid) {
				/*
				lastid ： 最后一条记录id
				weiduid ： 维度ID
				type :类型 ，这个就是零食，生活，饮食这些
				*/
				let that = this;
				let goodList = this.goodList;
				this.$http._post('examination/getconsumerlistbyweidu.do', {
					lastid,
					weiduid,
					type
				}).then(res => {
					if (res.data.issuccess == 1) {
						let {
							data
						} = res.data;
						let lastidData = that.lastidData;
						let len = data.length;
						for (let i = 0; i < len; i++) {
							data[i].gongxiao = data[i].gongxiao.split('\n');
							if (i == len - 1) {
								that.lastidData = data[i].id;
							}
						}
						if (data.length < 10 || data.length <= 0) {
							that.lastidData = -1;
						}
						if (goodList.length <= 0) {
							that.goodList = data;
						} else {
							goodList.push.apply(goodList, data);
							that.goodList = goodList;
						}
						// console.log(res)
					}
				})
			},
			goProduct(id) {
				this.isdetail  = true;
				if (!this.isLogin_s) {
					this.showIs = true;
					return
				}
				indexFx.basePoint(1, 0);
				// return
				let ty = -1;
				uni.navigateTo({
					url: `/second/yang/product/product?ty=${ty}&id=${id}`
				})
			},
			func(data) {
				this.isLogin_s = data.isLogin;
				let that = this;
				if (!data.isLogin) {
					this.showIs = false;
				} else {
					app.user = data.data;
					that.page = 1; //当前页
					that.pagesize = 10; //页码
					that.step = 0;
					that.goodList = [];
					that.isLogin_s = true;
					that.showIs = false;
					that.is=true;
					that.getInitialData()
				}
			},
			scrollLower() {
				console.log('滚动条触底')
				let that = this;
				if (!this.is) {
					this.$http.isMssage('暂无更多数据')
					return
				}
				that.page = that.page + 1;
				that.getAll(that.page, that.pagesize, that.step)

			}
		},
		onReachBottom() {
		
		}
	}
</script>

<style lang="scss">
	@import "../../common/css/flex.css";
	@import "../../common/css/index.scss";

	page {
		// background-color: #FFD344;
	}

	.tops {
		width: 100%;
		height: 102upx;

		.titleItem {
			width: 25%;
			height: 100%;
			font-size: 30upx;
			font-family: $typeface;
			font-weight: 500;
			color: #333333;
		}

		.selt {
			font-size: 36upx;
			font-family: $typeface;
			font-weight: 500;
			color: #333333;
		}
	}

	.body {
		width: 750upx;
		// min-height: 800px;
		height: 100vh;
		background: #F8F8F8;
		// border-radius: 48upx 48upx 0px 0px;
		// position: relative;
		padding-bottom: 16upx;

		.kongs {
			width: 100%;
			height: 10upx;
		}

		.sanjiaopng {
			width: 46upx;
			height: 16upx;
			position: absolute;
			top: -16upx;
			left: 65upx;
		}

		.san1 {
			left: 255upx;
		}

		.san2 {
			left: 440upx;
		}

		.san3 {
			left: 635upx;
		}

		.bodyItms {
			width: 100%;
			height: 150upx;

			.isss {
				width: 705upx;
				height: 100%;
				margin-left: 32upx;

				.Itms {
					width: 160upx;
					height: 54upx;
					background: #FFFFFF;
					border-radius: 32upx;
					font-size: 28upx;
					font-family: $typeface1;
					font-weight: 400;
					color: #666666;
					margin-right: 15upx;
					margin-bottom: 18upx;
				}

				.seltWe {
					color: #FFFFFF;
					background: #FF8F44;
				}
			}

		}

		.knobble {
			width: 710upx;
			min-height: 400upx;
			margin: auto;
			margin-top: 22upx;
			padding-bottom: 16upx;

			.items {
				width: 335upx;
				height: 464upx;
				background: #FFFFFF;
				border-radius: 16upx;
				margin-left: 13upx;
				margin-right: 8upx;
				margin-bottom: 16upx;

				.itmeImg {
					width: 333upx;
					height: 333upx;
					border-top-left-radius: 16upx;
					border-top-right-radius: 16upx;

					image {
						border-top-left-radius: 16upx;
						border-top-right-radius: 16upx;
						width: 333upx;
						height: 333upx;
					}
				}

				.itemText {
					width: 85%;
					height: 40upx;
					font-size: 28upx;
					font-family: $typeface;
					font-weight: 500;
					color: #333333;
					margin: 16upx 0;
					margin-left: 10upx;
				}

				.bootm {
					width: 100%;
					height: 39upx;
					margin-bottom: 20upx;

					.bootmItem {
						min-width: 46upx;
						padding: 0 10upx;
						height: 39upx;
						background: #FFFFFF;
						border-radius: 6upx;
						border: 1upx solid #BD9C30;
						font-size: 24upx;
						font-family: $typeface1;
						font-weight: 400;
						color: #BD9C30;
						margin-left: 10upx;
					}
				}
			}
		}
	}

	.yangKong {
		width: 100%;
		height: 130upx;
		background-color: #F8F8F8;
	}
</style>
